package net.sxnic.ugr.menu.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import net.sxnic.comm.CommConstant;
import net.sxnic.comm.utils.CommUtils;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 显示当前位置
 * 
 * @author 孙宇飞
 * 
 */
public class MenuCurrLocationTag extends BodyTagSupport {

	private static Logger logger = LoggerFactory
			.getLogger(MenuCurrLocationTag.class);

	private static final long serialVersionUID = -4045507557553115254L;

	private String menuCode;

	private String currLocation;

	public int doStartTag() throws JspException {
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() {
		JspWriter out = pageContext.getOut();
		try {
			
			if(StringUtils.isBlank(menuCode)){
				menuCode = (String)pageContext.getSession().getAttribute("menuCode");
			}
			
			if("0000".equals(menuCode)){
				out.print("");
				pageContext.getSession().removeAttribute("menuCode");
			}

			if (StringUtils.isBlank(menuCode)) {
				out.print(currLocation);
				pageContext.getSession().removeAttribute("menuCode");
				return EVAL_PAGE;
			}
			
			
			pageContext.getSession().setAttribute("menuCode",menuCode);
			
			if (CommUtils.isInt(menuCode)) {
				if (menuCode.length() == 2) {
					out.print(CommConstant.MENU_MAP.get(menuCode));
				} else if (menuCode.length() == 4) {
					out.print(CommConstant.MENU_MAP.get(StringUtils.left(
							menuCode, 2))
							+ " - "
							+ CommConstant.MENU_MAP.get(menuCode));
				} else if (menuCode.length() == 6) {
					out.print(CommConstant.MENU_MAP.get(StringUtils.left(
							menuCode, 2))
							+ " - "
							+ CommConstant.MENU_MAP.get(StringUtils.left(
									menuCode, 4))
							+ " - "
							+ CommConstant.MENU_MAP.get(menuCode));
				}
			}
		} catch (Exception e) {
			logger.error("====MenuCurrLocationTag===error====" + e.toString());
		}

		return EVAL_PAGE;
	}

	public String getMenuCode() {
		return menuCode;
	}

	public void setMenuCode(String menuCode) {
		this.menuCode = menuCode;
	}

	public String getCurrLocation() {
		return currLocation;
	}

	public void setCurrLocation(String currLocation) {
		this.currLocation = currLocation;
	}

}
